package jp.sourceforge.akjrcp.dropsomething2.psp;

import org.eclipse.core.runtime.Preferences.IPropertyChangeListener;
import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.part.ViewPart;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jp.sourceforge.akjrcp.akj_betas.Akj_betasPlugin;
import jp.sourceforge.akjrcp.akj_betas.Messages;
import jp.sourceforge.akjrcp.commons.ui.widget.LayoutDataBuilder;
import jp.sourceforge.akjrcp.commons.util.FileNameUtils;
import jp.sourceforge.akjrcp.dropsomething.FFMpegDropSomethingTab;
import jp.sourceforge.akjrcp.dropsomething.IDropSomething;
import jp.sourceforge.akjrcp.dropsomething2.AbstractDropFileGenericParametersOptionEditor;
import jp.sourceforge.akjrcp.dropsomething2.AbstractFFMpegDropSomething2;
import jp.sourceforge.akjrcp.dropsomething2.DefaultListControler;
import jp.sourceforge.akjrcp.dropsomething2.DropFileGenericParameterSetting;
import jp.sourceforge.akjrcp.dropsomething2.DropsomethingTab2;
import jp.sourceforge.akjrcp.dropsomething2.GenericParameterSetting;
import jp.sourceforge.akjrcp.dropsomething2.IDropSomething2;
import jp.sourceforge.akjrcp.dropsomething2.InfoDialog;
import jp.sourceforge.akjrcp.dropsomething2.ListControler;
import jp.sourceforge.akjrcp.dropsomething2.SearchBox;
import jp.sourceforge.akjrcp.dropsomething2.Setting;
import jp.sourceforge.akjrcp.dropsomething2.SettingControler;
import jp.sourceforge.akjrcp.dropsomething2.DefaultListControler.ListData;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegOptions;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegPreference;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegUI;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegUtils;
import jp.sourceforge.akjrcp.psp.PSPMovieSetting;
import jp.sourceforge.akjrcp.psp.PSPSettingPage;
import jp.sourceforge.akjrcp.ui.EditableListData;
import jp.sourceforge.akjrcp.ui.GenericParameters;
import jp.sourceforge.akjrcp.ui.GenericParametersEventListener;
import jp.sourceforge.akjrcp.ui.GenericParametersFFMpegPreference;
import jp.sourceforge.akjrcp.ui.GenericParametersTableViewer;
import jp.sourceforge.akjrcp.ui.param.ParamBoolean;
import jp.sourceforge.akjrcp.ui.param.ParamFile2;
import jp.sourceforge.akjrcp.util.StatusBarUtils;

public class PSPConvertTabBuilder2 implements IDropSomething{
	public static final String PREFERENCE_KEY="pspconvert2";
	public static final String PSP_HQ="PSP_HQ";
	private CTabFolder tab;
	private CTabItem settingTab;
	private CTabItem listTab;
		

	public static class SetPSPDir implements SelectionListener{
		PSPConvertTab2 tab;
		public SetPSPDir(PSPConvertTab2 tab){
			this.tab=tab;
		}
		public void widgetDefaultSelected(SelectionEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		public void widgetSelected(SelectionEvent e) {
			File roots[]=File.listRoots();
			for (int i = roots.length-1; i >0; i--) {
				File tmp=new File(roots[i],"PSP");
			if(tmp.exists()){
				File videoFolder=new File(roots[i],"VIDEO");
				if(videoFolder.exists()){
				//	log.trace("psp.exist");
					tab.setPSPDir(videoFolder);
					
				}
				}
			}
			
		}
		
	}
	
		 public class PSPConvertTab2 extends AbstractFFMpegDropSomething2{

			public PSPConvertTab2(Composite parent, int style, String key) {
				super(parent, style, key);
				
				updateVideoFilter();
			}
			
			public class DropFilterPreferenceStoreMonitor implements IPropertyChangeListener {

				public void propertyChange(PropertyChangeEvent event) {
					log.info(event.getProperty());
					if (event.getProperty().equals(PSPSettingPage.DROP_FILTER_VIDEO)) {
						updateVideoFilter();
					}
				}
			}
			
			private void updateVideoFilter(){
				String video=Akj_betasPlugin.getDefault().getPreferenceStore().getString(PSPSettingPage.DROP_FILTER_VIDEO);
				setSupportedFiles(video.split(" "));
			}
			
			public void setPSPDir(File videoFolder) {
				//TODO change method
				((ParamFile2)editor.getItem(DropFileGenericParameterSetting.EXPORT_DIR)).setEnabled(true);
				((ParamBoolean)editor.getItem(DropFileGenericParameterSetting.SAME_DIRECTORY)).setBoolean(false);
				((ParamFile2)editor.getItem(DropFileGenericParameterSetting.EXPORT_DIR)).setValue(videoFolder.getAbsolutePath());
				updateStatus();
			}

			protected void filterSaveParameters(Map<String ,String> map){
				//ignore dir and avisynth
				map.remove(DropFileGenericParameterSetting.BACKUP);
				map.remove(DropFileGenericParameterSetting.EXPORT_DIR);
				map.remove(DropFileGenericParameterSetting.INPUT_AUDIO1);
				map.remove(DropFileGenericParameterSetting.INPUT_VIDEO1);
				map.remove(DropFileGenericParameterSetting.SAME_DIRECTORY);
				map.remove(DropFileGenericParameterSetting.USE_AVISYNTH);
				//shortest is no need
				map.remove(FFMpegOptions.OPTION_SHORTEST);
			}

			@Override
			public GenericParameters makeDefaultGenericParameters() {
				return newGenericParameters();
			}

			
			
			
			public class CustomPSPEditor extends PSPOptionEditor{
				public Composite createOtherControls(Composite parent) {
					
					return parent;
				}
			}
			
			
		

			@Override
			protected void actionCancelUI() {
				//maybe nothig todo
			}


			@Override
			protected void actionFaildUI(Setting setting) {
				if(cancelled){
					listControler.cancelled(setting);
				}else{
					listControler.faild(setting);
				}
			}

			@Override
			protected void actionFinishUI() {
				listControler.finish();
			}

			@Override
			protected void actionStartUI() {
				tab.setSelection(listTab);
			}

			@Override
			protected void actionSuccessedUI(Setting setting) {
				listControler.successed(setting);
			}

			@Override
			public void afterExecutes() {
				// TODO �����������ꂽ���\�b�h�E�X�^�u
				//back to?tab
			}

			@Override
			public void beforeExecutes() {
				listControler.start();
			}

			@Override
			public ListControler createListControler(IDropSomething2 parent) {
				DefaultListControler list=new DefaultListControler(tab,SWT.NULL);
				list.setExecName("PSP");
				list.setDropSomething2(parent);
				list.addShowInfoListener(new ShowData());
				list.getQueList().addEditListener(new EditData());
				list.getQueList().addRemoveAllMenu();
				listTab.setControl(list);
				return list;
			}

			
			@Override
			public SettingControler createSettingControler(
					IDropSomething2 parent) {
				PSPSettingControler controler= new PSPSettingControler(tab,SWT.NULL);
				settingTab.setControl(controler);
				tab.setSelection(settingTab);
				return controler;
			}

			@Override
			protected void initialize(DropsomethingTab2 parent){
				this.setLayout(new FillLayout());
				
				
				tab = new CTabFolder(this,SWT.NULL);
				tab.setSimple(false);
				settingTab = new CTabItem(tab,SWT.NULL);
				settingTab.setText(Messages.getString("explorer.dd_new"));
				listTab = new CTabItem(tab,SWT.NULL);
				listTab.setText(Messages.getString("explorer.dd_working"));
				
				setCanConvert(true);
			}

	
			@Override
			public void setProgressText(String text) {//progress info
				getViewPart().getSite().getShell().getDisplay().asyncExec(new SetProgressText(listControler,text));
			}
	public  class ShowData implements SelectionListener{
				
				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
				}

				@Override
				public void widgetSelected(SelectionEvent e) {
					Setting setting=null;
					EditableListData data=((DefaultListControler)listControler).getCurrentData();
					setting=(Setting) data.getValue();
					
					Shell shell=new Shell(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
					shell.setText("setting");
					shell.setSize(660,480);
					
					PSPOptionDialog dialog=new PSPOptionDialog(shell,setting,false);
					
					int ret=dialog.open();
					
				}
				
			}
		
			public  class EditData implements SelectionListener{
				
				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
				}

				@Override
				public void widgetSelected(SelectionEvent e) {
					Setting setting=null;
					ISelection selection=((DefaultListControler)listControler).getQueList().getSelection();
					if(selection instanceof StructuredSelection){
						StructuredSelection s=(StructuredSelection)selection;
						Object[] items=s.toArray();
						for (int i = 0; i < items.length; i++) {
							ListData data=(ListData) items[i];
							setting=(Setting) data.getValue();
						}
					}
					Shell shell=new Shell(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
					shell.setText("setting");
					shell.setSize(660,480);
					
					PSPOptionDialog dialog=new PSPOptionDialog(shell,setting,true);
					
					int ret=dialog.open();
					
				}
				
			}

			public class PSPSettingControler extends Composite implements SettingControler{

				private Button editSetting;
				private Button undo;
				private Button redo;



				/*
				 * code is overwrapped AbstractFFMpegDropSomething2
				 */
				public PSPSettingControler(Composite parent, int style) {
					super(parent, style);
					this.setLayout(new FillLayout());
					Composite composite=new Composite(this,SWT.NULL);
					GridLayout rootLayout=new GridLayout(1,true);
					rootLayout.marginHeight=3;
					rootLayout.marginWidth=5;
					
					composite.setLayout(rootLayout);
					
					Akj_betasPlugin.getDefault().getPluginPreferences().addPropertyChangeListener( new PreferenceStoreMonitor());
					
					editor = makeGenericParameterOptionEditor(getPreferenceKey(),getPreferenceStore());
					
					
					((PSPOptionEditor)editor).setNewMode(true);
					((PSPOptionEditor)editor).setDirMode(true);
					//
					
					
					Composite list_editor=editor.createControls(composite);
					editor.addListener(new GenericParametersEventListener(){
						public void updateModified(boolean modified) {
							saveSetting.setEnabled(modified);
						}
						
					});
					
					Composite listComposite=new Composite(list_editor,SWT.NULL);
					GridData listData=new GridData(GridData.FILL_BOTH);
					
					SearchBox search=new SearchBox(listComposite,SWT.NULL);
					search.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
					
					listComposite.setLayoutData(listData);
					GridLayout listLayout=new GridLayout(1,false);
					listLayout.marginHeight=0;
					listLayout.marginWidth=0;
					listComposite.setLayout(listLayout);
					
					List list=GenericParametersTableViewer.loadList(getPreferenceStore().getString(getPreferenceKey()+"."+FFMpegDropSomethingTab.SETTINGS));
					
					GridData gd = new GridData(GridData.FILL_BOTH);
					//gd.minimumHeight=40;
					gd.heightHint=60;
					//gd.grabExcessVerticalSpace=true;
					tableViewer = new GenericParametersTableViewer(listComposite);
					search.setTarget(tableViewer);
					search.setTableOwner(PSPConvertTab2.this);
					
					tableViewer.getTable().setLayoutData(LayoutDataBuilder.fillBothGridData());
					tableViewer.setGenericParametersList(list);
					
					tableViewer.addSelectionChangedListener(new SelectChanged());
					
					Composite buttons=new Composite(listComposite,SWT.NULL);
					buttons.setLayout(new GridLayout(6,false));
					
					
					newSetting = new Button(buttons,SWT.NULL);
					newSetting.setText(Messages.getString("h264converter.new"));
					newSetting.addSelectionListener(new GenericNew());
					
					
					saveSetting = new Button(buttons,SWT.NULL);
					saveSetting.setText(Messages.getString("h264converter.save"));
					saveSetting.setEnabled(false);
					saveSetting.addSelectionListener(new GenericSave());
					
					
					editSetting = new Button(buttons,SWT.NULL);
					editSetting.setText(Messages.getString("ffmpegconvert.edit_name"));
					editSetting.addSelectionListener(new GenericEdit());
					//buttonList=new ArrayList();
					//settingList=new ArrayList();
					
					undo = new Button(buttons,SWT.NULL);
					undo.setEnabled(false);
					undo.setText(Messages.getString("undo"));
					undo.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							doUndo();
						}
					});
					addUndoControl(undo);
					redo = new Button(buttons,SWT.NULL);
					redo.setEnabled(false);
					redo.setText(Messages.getString("redo"));
					redo.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							doRedo();
						}
					});
					addRedoControl(redo);
					
				((AbstractDropFileGenericParametersOptionEditor)editor).getSelectDropFileButton().addSelectionListener(new SelectionListener(){
						public void widgetDefaultSelected(SelectionEvent e) {
						}

						public void widgetSelected(SelectionEvent e) {
							FileDialog dialog=new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),SWT.MULTI);
							//TODO come form settings
							dialog.setFilterExtensions(new String[]{"*.avi;*.flv;*.mpg;*.wmv;*.avs;*.mp4;*.vob;*.mpeg"});
							String path=dialog.open();
							if(path!=null){
								if(isCanConvert()){
								File parent=new File(path).getParentFile();
								String paths[]=dialog.getFileNames();
								for (int i = 0; i < paths.length; i++) {
									paths[i]=new File(parent,paths[i]).getAbsolutePath();
								}
								
								addDropFiles(paths);
								
								}else{
								StatusBarUtils.showStatusBar(getCantConvertStatusbarMessage());	
								}
							}
						}});
					
					
					
					Composite inputComposite=((PSPOptionEditor)editor).getInputComposite();
					
					Button startConvert=new Button(inputComposite,SWT.NULL);
					startConvert.setText(Messages.getString("ffmpegconvert.start_convert"));
					startConvert.setLayoutData(LayoutDataBuilder.hintGridData(200, 40));
					startConvert.addSelectionListener(new SelectionListener(){

						@Override
						public void widgetDefaultSelected(SelectionEvent e) {
							
						}

						@Override
						public void widgetSelected(SelectionEvent e) {
							
							PSPDropFileGenericParameterSetting base=new PSPDropFileGenericParameterSetting(editor.getParameters());
							String video=editor.getParameters().get(DropFileGenericParameterSetting.INPUT_VIDEO1);
							if(video.equals("")){
								return;
							}
							
							fixParameters(base);
							//event
							listControler.addSetting(base);
							
							//active working
							tab.setSelection(listTab);
							
							if(!listControler.isWorking() && listControler.isAutoStart()){
								startWorking();
								}
							else{
								actionStartUI();
							}
						}});
						
					Group option=new Group(inputComposite,SWT.NULL);
					option.setLayout(new GridLayout());
					option.setText(Messages.getString("ffmpegconvert.option"));
					Button showCommand=new Button(option,SWT.NULL);
					showCommand.addSelectionListener(new SelectionListener(){

						@Override
						public void widgetDefaultSelected(SelectionEvent e) {
							
						}

						@Override
						public void widgetSelected(SelectionEvent e) {
						
							PSPDropFileGenericParameterSetting base=new PSPDropFileGenericParameterSetting(editor.getParameters());
							String video=editor.getParameters().get(DropFileGenericParameterSetting.INPUT_VIDEO1);
							String audio=editor.getParameters().get(DropFileGenericParameterSetting.INPUT_AUDIO1);
							List<String> inputs=new ArrayList<String>();
							if(video!=null && !"".equals(video)){
								inputs.add(video);
							}
							if(audio!=null && !"".equals(audio)){
								inputs.add(audio);
							}
							if(inputs.size()==0){
								inputs.add("<INPUT>");
							}
							
							
							//make output
							//from executer
							String output="";
							
							if(inputs.get(0).equals("<INPUT>")){
								output="<OUTPUT>";
							}else{
							boolean sameDir=base.getBoolean(DropFileGenericParameterSetting.SAME_DIRECTORY);
							
							//TODO change extension
							String outFile=FileNameUtils.getChangedExtensionName(new File(inputs.get(0)), "mp4");
							
							if(!sameDir){
								output=new File(base.getParameters().get(DropFileGenericParameterSetting.EXPORT_DIR),new File(outFile).getName()).getAbsolutePath();
							}else{
								output=outFile;
							}
							}
							fixParameters(base);
						    
							FFMpegPreference preference=settingToFFMpegPreference(base);
							preference.setFfmpegPath(FFMpegUI.getFFmpegOrPureFFMpeg());
							String command=FFMpegUtils.toPrintCommand(preference.toCommand(inputs.toArray(new String[0]), output));
							
							InfoDialog dialog=new InfoDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
									Messages.getString("ffmpegconvert.command"),null,command,0,new String[]{"OK"},0);
							
							dialog.open();
						}});
					showCommand.setText(Messages.getString("ffmpegconvert.show_command"));
					
					updateStatus();
					
					
					Akj_betasPlugin.getDefault().getPluginPreferences().addPropertyChangeListener( new DropFilterPreferenceStoreMonitor());
					
					//select item
					int defaultSelect=getPreferenceStore().getInt(getPreferenceKey()+"."+SELECTION_INDEX);
					log.info("defaultSelect:"+defaultSelect);
					if(tableViewer.getTable().getItemCount()>defaultSelect){
						tableViewer.getTable().setSelection(Math.max(0,defaultSelect));//this method don't call SelectionChangedListener 
						
						}else{
							if(tableViewer.getTable().getItemCount()>0){
								tableViewer.getTable().setSelection(0);//this method don't call SelectionChangedListener 
							}else{
								log.info("item is empty");
							}
						}
					tableSelectionChanged();
					tableViewer.addSelectionChangedListener(new StoreSelect());
					
				
					
					createPopupMenu(tableViewer);
					
					//tableViewer.getTable().addKeyListener(new KeyActions());
				}

				//can catch if only focused
				public class KeyActions implements KeyListener{
					public void keyPressed(KeyEvent e) {
						System.out.println(e.character);
					}

					@Override
					public void keyReleased(KeyEvent e) {
			
					}
				}
				


				@Override
				public Object getControler() {
					return this;
				}
				
				

				public Setting[] getSettings(String[] files) {
					List<Setting> settings=new ArrayList<Setting>();
					
					PSPDropFileGenericParameterSetting base=new PSPDropFileGenericParameterSetting(editor.getParameters());
					
		
					
					for (int i = 0; i < files.length; i++) {
						File f=new File(files[i]);
						addFiles(settings,base,f);
					}
					
					
					return settings.toArray(new Setting[settings.size()]);
				}
				
			}
			
		
				
			private  void addFiles(List<Setting> settings,PSPDropFileGenericParameterSetting base,File file){
				
				if(file.isDirectory()){
					String list[]=file.list();
					if(list!=null){
						for (int i = 0; i < list.length; i++) {
							File f=new File(file,list[i]);
							if(f.isDirectory()){
								addFiles(settings,base,f);
							}else{
							String ext=FileNameUtils.getExtension(list[i]);
							if(isSuppotedExtension(ext)){
								addFiles(settings,base,f);
								}
							}
						}
					}
				}else{
					PSPDropFileGenericParameterSetting param=(PSPDropFileGenericParameterSetting) base.clone();
					param.set(PSPDropFileGenericParameterSetting.INPUT_AUDIO1, "");	//clear audio
					param.set(PSPDropFileGenericParameterSetting.INPUT_VIDEO1, file.getAbsolutePath());
					
						
					fixParameters(param);
					
					settings.add(param);
				}
			}
			
			private void fixParameters(PSPDropFileGenericParameterSetting param){
				param.set("EXTENSION", "mp4");
			}


			@Override
			public FFMpegPreference settingToFFMpegPreference(Setting setting) {
				//put first for twopassencode 
				//see GenericParametersFFMpegPreference L49
				setting.getParameters().put(FFMpegOptions.OPTION_FORMAT, FFMpegOptions.FORMAT_PSP);
				
				GenericParametersFFMpegPreference generic=
				new GenericParametersFFMpegPreference((GenericParameterSetting)setting);
				if(setting.getBoolean(PSP_HQ)){
					String options[]=FFMpegPreference.parseCommand(Akj_betasPlugin.getDefault().getPreferenceStore().getString(PSPSettingPage.HQ_SETTINGS));
					generic.setAddOptions(options);
				}
				//force psp settings
				//-format
				
				setting.getParameters().put(FFMpegOptions.OPTION_CODER, "1");//PSP need it
				setting.getParameters().put(FFMpegOptions.OPTION_FLAGS, FFMpegOptions.FLAGS_BITEXACT);
				
				
				//-codec
				if(setting.getString("vcodec").equals("")){//maybe not happen
					setting.getParameters().put(FFMpegOptions.OPTION_VIDEO_CODEC, FFMpegOptions.CODEC_VIDEO_H264);
					
				}
				setting.getParameters().put(FFMpegOptions.OPTION_AUDIO_CODEC, FFMpegOptions.CODEC_AUDIO_AAC);
				
				//force size
				/*
				String notsupportedSize[]={"480x272","720x480","352x480","480x270","360x270"};
				if(setting.getString("vcodec").equals("xvid")){
					String s=setting.getString("s");
					for (int i = 0; i < notsupportedSize.length; i++) {
						if(s.equals(notsupportedSize[i])){
							setting.getParameters().put("s","368x270");
						}
					}
					
				}
				*/
				
				//-level
				String vcodec=setting.getString("vcodec");

				if(vcodec.equals("h264")){
					setting.getParameters().put(FFMpegOptions.OPTION_LEVEL, ""+FFMpegOptions.LEVEL_21);
					//some ffmpeg make error happen
					setting.getParameters().put(FFMpegOptions.OPTION_RC_EQ, ""+FFMpegOptions.RC_EQ_X264);
				}else{
					setting.getParameters().put(FFMpegOptions.OPTION_VTAG, FFMpegOptions.TAG_MP4VIDEO);
				}
				String bitrate=setting.getString("b");
				if(!bitrate.endsWith("k")){
					bitrate="368k";
				}
				int bit=Integer.parseInt(bitrate.substring(0,bitrate.length()-1));
				setting.getParameters().put(FFMpegOptions.OPTION_VIDEO_BITRATE,""+(bit-8)+"k");
				setting.getParameters().put(FFMpegOptions.OPTION_MAXRATE,""+(bit)+"k");
				setting.getParameters().put(FFMpegOptions.OPTION_BUFSIZE,""+(bit)+"k");
				
				String size=setting.getString(FFMpegOptions.OPTION_SIZE);
				if(size==null){
					size="368x270";
				}
				
				//set level 30
				if(size.equals(PSPMovieSetting.SIZE_368x272)){
					setting.getParameters().put(FFMpegOptions.OPTION_LEVEL,""+FFMpegOptions.LEVEL_30);
				}else if(size.equals(PSPMovieSetting.SIZE_720x480)){
					setting.getParameters().put(FFMpegOptions.OPTION_LEVEL,""+FFMpegOptions.LEVEL_30);
				}else if(size.equals(PSPMovieSetting.SIZE_480x272)){
					setting.getParameters().put(FFMpegOptions.OPTION_LEVEL,""+FFMpegOptions.LEVEL_30);
				}else if(size.equals(PSPMovieSetting.SIZE_352x480)){
					setting.getParameters().put(FFMpegOptions.OPTION_LEVEL,""+FFMpegOptions.LEVEL_30);
				}
				
				//when level 30 maxrate and bufsize up,but i dont know why it need any more.
				if(setting.getInt(FFMpegOptions.OPTION_LEVEL)==FFMpegOptions.LEVEL_30){
					if(bit>768 && bit<10000){
					setting.getParameters().put(FFMpegOptions.OPTION_MAXRATE,""+(bit*4)+"k");
					setting.getParameters().put(FFMpegOptions.OPTION_BUFSIZE,""+(bit*4)+"k");
					}
				}
				
				//make thumbnail
				if(Akj_betasPlugin.getDefault().getPluginPreferences().getBoolean(PSPSettingPage.MAKE_THUMBNAIL)){
					setting.getParameters().put(GenericParametersFFMpegPreference.AFTER_ACTION,"jp.sourceforge.akjrcp.dropsomething.actions.MakePSPThumbnailAction");
				}
				return generic;
			}

			@Override
			public AbstractDropFileGenericParametersOptionEditor makeGenericParameterOptionEditor(
					String preferenceKey, IPreferenceStore preference) {
				CustomPSPEditor editor=new CustomPSPEditor();
				editor.setPreferenceKey(preferenceKey);
				editor.setPreferenceStore(preference);
				editor.setPspConvertTab2(this);
				return  editor;
			}
			 
		 }
	
		 public class SetProgressText extends Thread{
			 public String text;
			 public ListControler listControler;
			 public SetProgressText(ListControler listControler,String text){
				 this.text=text;
				 this.listControler=listControler;
			 }
			 public void run(){
				 listControler.setProgressText(text);
			 }
		 }

			//private DropsomethingTab dtab;
			private PSPConvertTab2 dtab;
			private CTabItem tabItem1 ;
			
			public void createControl(final ViewPart part,final CTabFolder tab) {
				if(dtab==null){
					//deley create control
					dtab=new PSPConvertTab2(tab,SWT.NULL,PREFERENCE_KEY);
					dtab.setViewPart(part);
					

					//support undo and redo
					part.getViewSite().getActionBars().setGlobalActionHandler(ActionFactory.UNDO.getId(),
							dtab.createUndoAction());
					part.getViewSite().getActionBars().setGlobalActionHandler(ActionFactory.REDO.getId(),
							dtab.createRedoAction());
					
					tabItem1.setControl(dtab);
				}
			}
			

	public void createTab(ViewPart part, CTabFolder tab) {
		 tabItem1 = new CTabItem(tab, SWT.NONE);
		
		//custom below
		tabItem1.setText(Messages.getString("pspconvert.tab"));
		
	
	}
	
	public static GenericParameters newGenericParameters(){
		GenericParameters param=new GenericParameters();
		param.set("EXTENSION", "mp4");
		param.set("vcodec", "h264");
		param.set("s", "368x272");
		param.set("MAX_FRAME_RATE", "30");
		param.set("b", "768k");
		param.set("ab", "128k");
		param.set("ar", "48000");
		param.set("ac", "2");
		param.set("PSP_HQ", "1");
		
		return param;
	}
	


}
